package com.datashare.dataentryeast.controller;

import com.datashare.dataentryeast.domain.ILpyg;
import com.datashare.dataentryeast.service.ILpygService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 一级菜单示例信息录入Controller
 *
 * @author lhj
 * @date 2021-12-22
 */
@RestController
@RequestMapping("/lpry/lpyg")
public class ILpygController extends BaseController {
    @Autowired
    private ILpygService ILpygService;

    /**
     * 查询一级菜单示例信息录入列表
     */
    @PreAuthorize("@ss.hasPermi('lpry:lpyg:list')")
    @GetMapping("/list")
    public TableDataInfo list(ILpyg lpyg) {
        startPage();
        List<ILpyg> list = ILpygService.selectLpygList(lpyg);
        return getDataTable(list);
    }

    /**
     * 导出一级菜单示例信息录入列表
     */
    @PreAuthorize("@ss.hasPermi('lpry:lpyg:export')")
    @Log(title = "一级菜单示例信息录入", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ILpyg lpyg) {
        List<ILpyg> list = ILpygService.selectLpygList(lpyg);
        ExcelUtil<ILpyg> util = new ExcelUtil<ILpyg>(ILpyg.class);
        util.exportExcel(response, list, "一级菜单示例信息录入数据");
    }

    /**
     * 获取一级菜单示例信息录入详细信息
     */
    @PreAuthorize("@ss.hasPermi('lpry:lpyg:query')")
    @GetMapping(value = "/{lpygId}")
    public AjaxResult getInfo(@PathVariable("lpygId") Long lpygId) {
        return AjaxResult.success(ILpygService.selectLpygByLpygId(lpygId));
    }

    /**
     * 新增一级菜单示例信息录入
     */
    @PreAuthorize("@ss.hasPermi('lpry:lpyg:add')")
    @Log(title = "一级菜单示例信息录入", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ILpyg lpyg) {
        return toAjax(ILpygService.insertLpyg(lpyg));
    }

    /**
     * 修改一级菜单示例信息录入
     */
    @PreAuthorize("@ss.hasPermi('lpry:lpyg:edit')")
    @Log(title = "一级菜单示例信息录入", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody ILpyg lpyg) {
        return toAjax(ILpygService.updateLpyg(lpyg));
    }

    /**
     * 删除一级菜单示例信息录入
     */
    @PreAuthorize("@ss.hasPermi('lpry:lpyg:remove')")
    @Log(title = "一级菜单示例信息录入", businessType = BusinessType.DELETE)
    @DeleteMapping("/{lpygIds}")
    public AjaxResult remove(@PathVariable Long[] lpygIds) {
        return toAjax(ILpygService.deleteLpygByLpygIds(lpygIds));
    }

    /**
     * 导入一级菜单示例信息录入
     */
    @Log(title = "一级菜单示例信息录入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('lpry:lpyg:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<ILpyg> util = new ExcelUtil<ILpyg>(ILpyg.class);
        List<ILpyg> dataList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = ILpygService.importLpyg(dataList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 下载导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<ILpyg> util = new ExcelUtil<ILpyg>(ILpyg.class);
        util.importTemplateExcel(response, "一级菜单示例信息录入数据");
    }
}
